﻿using System;
using System.Windows;
using System.Data.SqlClient;
using System.Windows.Forms;
using tb = Hardcodet.Wpf.TaskbarNotification;
using System.Windows.Threading;
using System.Data.SqlTypes;



namespace black_screen_mode_server
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        private string userName;
        bool isLogin = false;

        public LoginWindow()
        {
            InitializeComponent();
            //if (!ProjectTools.Verify())
            //{
            //    System.Windows.MessageBox.Show("注册信息错误，请重新注册！", "提示");
            //    System.Windows.Application.Current.Shutdown();
            //}
        }

        private string connectionString = ProjectTools.configDbConnString; //"server=LAPTOP-CKCFDD72;Database=TestDatabase;User Id=sa;Password=bageling;";


        //点击登录
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string username = txtUersname.Text.Trim();
            string password = txtPassword.Password.Trim();
            this.userName = txtUersname.Text.Trim();
            MainWindow mainWindow = null;

            foreach (Window window in System.Windows.Application.Current.Windows)
            {
                if (window is MainWindow)
                {
                    mainWindow = (MainWindow)window;
                    mainWindow.setUserName(this.userName);
                    mainWindow.JudgeAccount(this.userName);
                    //登陆后主界面默认显示TimerFormPage
                    mainWindow.setDefaultView();
                    break;
                }
            }

            if (mainWindow == null)
            {
                try
                {
                    mainWindow = new MainWindow(this.userName);
                }
                catch (Exception)
                {
                    System.Windows.MessageBox.Show("初始化失败，请检查相关配置！", "提示");
                    return;
                }

            }

            if (ValidateLogin(username, password))
            {
                isLogin = true;
                mainWindow.Visibility = Visibility.Visible;
                this.Close();
            }
            else
            {
                System.Windows.MessageBox.Show("用户名或密码错误!", "提示");
            }
        }

        //登录及权限
        private bool ValidateLogin(string username, string password)
        {
            try
            {
                SqlHelper sqlHelper = new SqlHelper(ProjectTools.configDbConnString);
                string loginSql = $"SELECT COUNT(*) From Users WHERE  Username = '{username}' AND Password = '{ProjectTools.encrypt(password)}'";
                Logger.Singleton.Info("查询sql：" + loginSql);
                int cnt = sqlHelper.ExecuteScalar(loginSql);
                return cnt > 0;

                //using (SqlConnection connection = new SqlConnection(connectionString))
                //{
                //    string query = "SELECT COUNT(*) From Users WHERE  Username = @Username AND Password = @Password";
                //    SqlCommand command = new SqlCommand(query, connection);
                //    command.Parameters.AddWithValue("@Username", username);
                //    string encryptPwd = ProjectTools.encrypt(password);
                //    Logger.Singleton.Info("查询语句："+query);
                //    command.Parameters.AddWithValue("@Password", encryptPwd);

                //    connection.Open();
                //    int count = (int)command.ExecuteScalar();
                //    return count > 0;
                //}
            }
            catch (Exception)
            {
                Logger.Singleton.Error("登录验证：数据库连接失败！");
                return false;

            }

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!isLogin)
                System.Windows.Application.Current.Shutdown();
        }
    }


}
